<?php
namespace app\common\logic;
use think\Db;
use think\Exception;

class SslPayment
{
    private static $_instance;
    
    private function __construct() {}
    
    public static function getInstance()
    {
        if(!(self::$_instance instanceof self)) {
            self::$_instance = new self;
        }
        return self::$_instance;
    }

    public function dealInfo($data) {
        $kjjg_arr = str_split(trim($data['kjjg']),1);
        $code_arr = explode(',',$data['tzhm']);
        $kjjg_1 = $kjjg_arr[0];
        $kjjg_2 = $kjjg_arr[1];
        $kjjg_3 = $kjjg_arr[2];
        $kjjg_he = $kjjg_arr[0]+$kjjg_arr[1]+$kjjg_arr[2];
        $zjzs = 0;
        $zjje = 0;
	try{
        switch($data['wfid']) {
        	case '142':
        		if(count($code_arr)==3){
        			if(strpos($code_arr[0],$kjjg_1) !== false){
        				if(strpos($code_arr[1],$kjjg_2) !== false){
        					if(strpos($code_arr[2],$kjjg_3) !== false){
        						$zjzs=$zjzs+1;
        					}
        				}
        			}
        		}
                break;
        	case '143':
        		if(strpos($data['tzhm'], $kjjg_he)!==false){
        			$zjzs=$zjzs+1;
        		}
        		break;
        	case '144':
        		if(($kjjg_1==$kjjg_2 && $kjjg_1!=$kjjg_3) || ($kjjg_2==$kjjg_3 && $kjjg_1!=$kjjg_2) || ($kjjg_1==$kjjg_3 && $kjjg_1!=$kjjg_2)){
        			if(strpos($data['tzhm'],$kjjg_1) !== false){
        				if(strpos($data['tzhm'],$kjjg_2) !== false){
        					if(strpos($data['tzhm'],$kjjg_3) !== false){
        						$zjzs=$zjzs+1;
        					}
        				}
        			}
        		}
        		break;
        	case '145':
        		if($kjjg_1!=$kjjg_2 && $kjjg_1!=$kjjg_3 && $kjjg_2 != $kjjg_3){
        			if(strpos($data['tzhm'],$kjjg_1) !== false){
        				if(strpos($data['tzhm'],$kjjg_2) !== false){
        					if(strpos($data['tzhm'],$kjjg_3) !== false){
        						$zjzs=$zjzs+1;
        					}
        				}
        			}
        		}
        		break;
        	case '146':
        		if(count($code_arr)==2){
        			if(strpos($code_arr[0],$kjjg_2) !== false){
        				if(strpos($code_arr[1],$kjjg_3) !== false){
        					$zjzs=$zjzs+1;
        				}
        			}
        		}
        		break;
        	case '147':
        		$zh = $kjjg_2+$kjjg_3;
        		if(strpos($data['tzhm'], $zh) !== false){
        			$zjzs =$zjzs+1;
        		}
        		break;
        	case '148':
        		if(strpos($data['tzhm'],$kjjg_2) !== false){
        			if(strpos($data['tzhm'],$kjjg_3) !== false){
        				$zjzs=$zjzs+1;
        			}
        		}
        		break;
        	case '149':
        		if(count($code_arr)==2){
        			if(strpos($code_arr[0],$kjjg_2) !== false){
        				if(strpos($code_arr[1],$kjjg_1) !== false){
        					$zjzs=$zjzs+1;
        				}
        			}
        		}
        		break;
        	case '150':
        		$zh = $kjjg_2+$kjjg_1;
        		if(strpos($data['tzhm'], $zh) !== false){
        			$zjzs =$zjzs+1;
        		}
        		break;
        	case '151':
        		if(strpos($data['tzhm'],$kjjg_2) !== false){
        			if(strpos($data['tzhm'],$kjjg_1) !== false){
        				$zjzs=$zjzs+1;
        			}
        		}
        		break;
        	case '152':
        		if(count($code_arr)==3){
        			if(strpos($code_arr[0],$kjjg_1) !== false){
        				$zjzs=$zjzs+1;
        			}
        			if(strpos($code_arr[1],$kjjg_2) !== false){
        				$zjzs=$zjzs+1;
        			}
        			if(strpos($code_arr[2],$kjjg_3) !== false){
        				$zjzs=$zjzs+1;
        			}
        		}
        		break;
        	case '153':
        		$old = explode(' ',$data['tzhm']);
        		foreach ($old as $v){
        			if((strpos($v,$kjjg_1) !== false) || (strpos($v,$kjjg_2) !== false) || (strpos($v,$kjjg_3) !== false)){
        				$zjzs+=1;
        			}
        		}
        		break;
        }
	}catch ( Exception $e ){
		echo $e->getMessage() ;
		return false;
	}
        //结算中奖并转移结算数据
        if($data['nb']==1 || $data['swzh']==1) {
            $table = 'touzhu_list_sw';
        } else {
            $table = 'touzhu_list_ok';
        }
        
        //选择投注模式
        if($data['nbzh'] == 1) $pieces = 1;
        if($data['nbzh'] == 2) $pieces = 10;
        if($data['nbzh'] == 3) $pieces = 100;
        
        //处理中奖金额（赔率*单注金额*中奖注数/投注模式），特殊玩法除外
        $zjje = $data['dqpl'] * $data['dzje'] * $zjzs / $pieces;
        
        //转移投注注单
        $insert_data = $data;
		$is_zh = $insert_data['from'];

        unset($insert_data['user_id'],$insert_data['swzh'],$insert_data['nb'],$insert_data['rktime'],$insert_data['cp_name_old'],$insert_data['cp_name_new'],$insert_data['from']);
        $insert_data['ifqs'] = '1';
        $insert_data['zjje'] = $zjje;
        $insert_data['zjzs'] = $zjzs;
        
        $insert_pc = $insert_data;
        unset($insert_pc['tzhje'],$insert_pc['tz_time']);
        $tzhje = Db::name('username')->where('id',$data['user_id'])->value('je');
        $insert_pc['tzhje'] = $tzhje;
        $insert_pc['tz_time'] = time();
        
        Db::startTrans();
        try {
        	if(!Db::name($table)->data($insert_data)->insert()){
        		throw new \Exception(false);
        	}
        	
        	if(!Db::name('touzhu_list_no')->where('id',$insert_data['id'])->delete()){
        		throw new \Exception(false);
        	}
        	
        	//中奖金额大于0.001则给用户增加金额
        	if($zjje > 0.001) {
        		if(!Db::name('touzhu_list_pc')->data($insert_pc)->insert()){
        			throw new \Exception(false);
        		}
        		//$prefix = config('database.prefix');
        		//Db::execute("UPDATE {$prefix}username SET  je=je+'{$zjje}',zjzje=zjzje+'{$zjje}' WHERE id={$data['user_id']}");
        		if(!Db::name('username')->where('id',$data['user_id'])->update(['je'=>['exp',"je+{$zjje}"],'zjzje' =>['exp',"zjzje+{$zjje}"]])){
        			throw new \Exception(false);
        		}
				//处理追号自动停止
				if( 1==intval($is_zh) ){
					$list_zh = Db::name('touzhu_list_zh')->field('*,1 as type')->where(['qihao'=>['>',$data['qihao']]])->where(['czid'=>$data['czid'],'wfid'=>$data['wfid'],'stop'=>1])->select();
					$list_no = Db::name('touzhu_list_no')->field('*,2 as type')->where(['qihao'=>['>',$data['qihao']]])->where(['czid'=>$data['czid'],'wfid'=>$data['wfid'],'from'=>1])->select();
					$list = array_merge($list_zh,$list_no);
					$tkqje = Db::name('username')->where('id',$data['user_id'])->value('je');
                    //$tkqje = floatval($tzhje) + floatval($zjje);
					foreach($list as $v){
						$id = $v['id'];
						unset($v['id']);
						if( 1==$v['type'] ){
							unset($v['type']);
							unset($v['stop']);
							if( !Db::name('touzhu_list_zh')->delete($id) ){
								throw new \Exception(false);
							}
						}else{
							unset($v['rktime']);
							unset($v['type']);
							unset($v['from']);
							if( !Db::name('touzhu_list_no')->delete($id) ){
								throw new \Exception(false);
							}
						}
						$v['tktime'] = time();
						$v['tkqje'] = $tkqje;
						$tkqje += floatval($v['tzje']);

						if( !Db::name('touzhu_list_tk')->insert($v) ){
							throw new \Exception(false);
						}
						if( !Db::name('username')->where('id',$data['user_id'])->update(array('je'=>['exp',"je+'{$v['tzje']}'"],'tzzje'=>['exp',"tzzje-'{$v['tzje']}'"])) ){
							throw new \Exception(false);
						}
					}
				}
        	}
            /* Db::name($table)->data($insert_data)->insert();
            Db::name('touzhu_list_no')->where('id',$insert_data['id'])->delete();
            //中奖金额大于0.001则给用户增加金额
            if($zjje > 0.001) {
                //$prefix = config('database.prefix');
                //Db::execute("UPDATE {$prefix}username SET  je=je+'{$zjje}',zjzje=zjzje+'{$zjje}' WHERE id={$data['user_id']}");
                Db::name('username')->where('id',$data['user_id'])->update(['je'=>['exp',"je+{$zjje}"],'zjzje' =>['exp',"zjzje+{$zjje}"]]);
            } */
            Db::commit();
            return $zjje;
        } catch (\Exception $e) {
            Db::rollback();
            return false;
        }
        
    }
}